(function ($) {
  "use strict";
  /*
  * author GL
  * params = {
  * }
  * */
  $.contact = function (option) {
    var defaults = {
      type: null, // 选择类型
      totality: 0, // 可选个数
      list: [], // 联系人列表
      target: null, // 联系人展示列表
      typeStr: {adult: '成人',child: '儿童',elder: '长者'},
      confirmFn: function () {},
      editsaveFn: function () {}
    };
    var params = $.extend({},defaults,option);
    params.target.each(function () {
      params.list[$(this).data("index")].checkType = $(this).data("type");
      params.list[$(this).data("index")].checkState = 'check';
    })
    var select = _.template($("#contact-select").html())(params);
    var contact = $(select).appendTo(document.body);
    $.pushHistory();
    contact.show().addClass("show");
    // 选择
    $(".gzl-flex_acenter", contact).on("click", function () {
      var icon = $(this).find("use");
      var checkStatus = icon.attr("xlink:href");
      if(checkStatus !== "#icon-checked"){
        if (checkStatus === "#icon-uncheck") {
          icon.attr("xlink:href","#icon-check").parent().addClass("checked");
        }else if (checkStatus === "#icon-check"){
          icon.attr("xlink:href","#icon-uncheck").parent().removeClass("checked");
        }
      }
    })
    // 确认
    $(".confirm-btn", contact).on("click", function () {
      var tourist = {
        type: params.type,
        typeStr: params.typeStr[params.type],
        list: []
      };
      $(".contact-check .checked", contact).each(function (i) {
        if ($(this).find("use").attr("xlink:href") === "#icon-check"){
          var contactIndex = $(this).parents(".contact-item").data("index");
          tourist.list[i] = params.list[contactIndex];
        }
      })
      // 修正证件号码 变成 440111****1324;
      function hideCardNum(obj){
        for(var i=0;i<obj.list.length;i++){
          var cardnum = obj.list[i].cardNm
          if(cardnum){
            obj.list[i].cardNm = cardnum.substr(0,5) + '****' + cardnum.substr(parseInt(cardnum.length-4))
          }
        }
        return obj
      }
      params.confirmFn(hideCardNum(tourist),contact);
    })
    // 取消
    $(".cancel-btn", contact).on("click", function () {
      contact.hidePopup()
    })
    // 编辑
    $(".contact-edit,.contact-add", contact).on("click", function () {
      $.pushHistory();
      var isedit = $(this).hasClass("contact-edit");
      var editData = null;
      if(isedit){
        editData = params.list[$(this).parent().data("index")]
        console.log(editData)
      }
      $.formedit({
        saveFn: function () {
          params.editsaveFn()
        },
        form: [{
          item: '基本信息',
          list: [
            {
              type: 'text',
              name: '中文姓',
              placeholder: '请与证件保持一致',
              required: true
            },
            {
              type: 'text',
              name: '中文名',
              placeholder: '请与证件保持一致',
              required: true
            },
            {
              type: 'text',
              name: '英文姓',
              placeholder: '请与证件保持一致'
            },
            {
              type: 'text',
              name: '英文名',
              placeholder: '请与证件上名一致'
            },
            {
              type: 'text',
              name: '手机号码',
              placeholder: '请输入手机号码',
              required: true
            },
            {
              type: 'text',
              name: '电子邮箱',
              placeholder: '请输入邮箱(选填)'
            }
          ]
        },{
          item: '证件信息',
          list: [
            {
              type: 'code',
              name: '身份证',
              placeholder: '请输入证件号码'
            },
            {
              type: 'date',
              name: '证件有效期',
              placeholder: '请选择有效期'
            }
          ]
        },{
          item: '其他信息',
          list: [
            {
              type: 'date',
              name: '生日',
              placeholder: '请与证件保持一致'
            },
            {
              type: 'radio',name: '性别',
              item: {
                name: 'sex',
                option: ['男','女']
              }
            }
          ]
        }]
      });
    })
  };
})($);